package com.alvis.media.controller.admin;

import com.alvis.media.base.BaseApiController;
import com.alvis.media.base.RestResponse;
import com.alvis.media.domain.User;
import com.alvis.media.domain.VideoInfo;
import com.alvis.media.domain.VideoPlay;
import com.alvis.media.service.UserEventLogService;
import com.alvis.media.service.UserService;
import com.alvis.media.service.VideoInfoService;
import com.alvis.media.service.VideoPlayService;
import com.alvis.media.utility.DateTimeUtil;
import com.alvis.media.viewmodel.admin.dashboard.IndexVM;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@RestController("AdminDashboardController")
@RequestMapping(value = "/api/admin/dashboard")
@AllArgsConstructor
public class DashboardController extends BaseApiController {

    private final UserService userService;
    private final VideoInfoService videoInfoService;
    private final VideoPlayService videoPlayService;
    private final UserEventLogService userEventLogService;


    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public RestResponse<IndexVM> Index() {
        IndexVM vm = new IndexVM();

        // 查询本月新增用户，
        // step1:字段封装
        //  step2：调用service层得到结果
        int count = userService.selectUserCount(new User());
        // step3: 将结果封装返回给前端。
        vm.setNewUserCount(count);



        // 查询本月新增视频数
        // 1.调用service 去数据库查找数据
        int newVideoCount = videoInfoService.selectNewVideoCount(new VideoInfo());

        // 2.封装返回信息
        vm.setNewVideoCount(newVideoCount);

        // 查询本月视频播放数
        // 1.调用service 去数据库查找数据
        int videoPlayCount = videoPlayService.selectVideoPlayCount(new VideoPlay());

        // 2.封装返回信息
        vm.setDoPlayVideoCount(videoPlayCount);

        // 查询本月最佳影片
        // 1.调用service 去数据库查找数据
        String bestVideo = videoPlayService.selectBestVideo(new VideoPlay());

        // 2.封装返回信息
        vm.setHotVideoCount(bestVideo);

        List<Integer> mothDayUserActionValue = userEventLogService.selectMothCount();

        List<Integer> mothDayVideoPlayValue = videoPlayService.selectMothCount();

        vm.setMothDayUserActionValue(mothDayUserActionValue);
        vm.setMothDayVideoPlayValue(mothDayVideoPlayValue);

        vm.setMothDayText(DateTimeUtil.monthDay());

        BufferedReader reader =null;
        InputStream inputStream =null;
        try {
            // 1. 加载配置文件
            Properties properties = new Properties();
            // 配置文件放在resources目录下，使用类加载器读取
            inputStream = getClass().getClassLoader().getResourceAsStream("python-config.properties");
            if (inputStream == null) {
                throw new FileNotFoundException("配置文件 python-config.properties 未找到");
            }
            properties.load(inputStream);

            // 2. 从配置文件获取路径
            String pythonInterpreter = properties.getProperty("python.interpreter.path");
            String pythonScript = properties.getProperty("python.script.path");

            // 3. 定义命令数组
            String[] cmd = {pythonInterpreter, pythonScript};

            // 4. 启动进程执行命令
            Process process = new ProcessBuilder(cmd)
                    .redirectErrorStream(true)  // 合并错误流到输入流
                    .start();

            // 5. 读取Python的输出
            reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream())
            );

            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("Python输出：" + line);
            }

            // 6. 等待脚本执行完成
            int exitCode = process.waitFor();
            System.out.println("脚本执行结束，退出码：" + exitCode);




        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // 关闭资源
            if (reader!=null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }



            }
        }
        return RestResponse.ok(vm);


    }

}